import { ItemData } from "../../resource/info/ItemData";
import { MWPropertyItemData } from "./data/MWPropertyItemData";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { MagicWeaponConfig } from "../../resource/MagicWeaponConfig";
import { EPlayerItemPosType } from "../../../../Message/Public/EPlayerItemPosType";
import { MagicWeaponItemData } from "./MagicWeaponItemData";
import { ECategory } from "../../../../Message/Public/ECategory";
import { Cache } from "../../cache/Cache";
import { EActiveType } from "../../../../Message/Public/EActiveType";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { MsgManager } from "../../manager/MsgManager";
import { Language } from "../../../../extend/language/Language";
type int = number;
//class MagicWeaponUtil
    
    export  class MagicWeaponUtil
    {
        

        public static analyseProperty(arg1: ItemData, arg2: Boolean=false): Array<any>
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc5=null;
            var loc6=null;
            var loc8=null;
            var loc9=null;
            var loc10=null;
            var loc11=null;
            var loc12=null;
            var loc7=[];
            if (arg1) 
            {
                if (arg2) 
                {
                    loc10 = arg1.itemExInfo.al_rf_m_attr;
                    loc11 = arg1.itemExInfo.al_rf_as_attr;
                }
                else 
                {
                    loc10 = arg1.itemExInfo.al_m_attr;
                    loc11 = arg1.itemExInfo.al_as_attr;
                }
                if (loc10) 
                {
                    loc9 = (loc10 = (loc10 = loc10.replace(new RegExp("\\[", "g"), "")).replace(new RegExp("\\]", "g"), "")).split(",");
                    (loc8 = new MWPropertyItemData()).name = GameDefConfig.instance.getItem("MagicWeapon", 0).name;
                    loc8.type = 0;
                    loc8.property = loc9[1];
                    loc8.typeName = MWPropertyItemData.TypeName_Main;
                    loc7.push(loc8);
                }
                if (loc11) 
                {
                    loc1 = loc11.replace(new RegExp("\\[", "g"), "");
                    if (loc2 = loc1.split("]")) 
                    {
                        loc3 = (loc2.length - 1);
                        loc4 = 0;
                        while (loc4 < loc3) 
                        {
                            if (loc5 = loc2[loc4]) 
                            {
                                if (loc6 = loc5.split(",")) 
                                {
                                    loc12 = GameDefConfig.instance.getItem("MagicWeapon", int(loc6[0]));
                                    (loc8 = new MWPropertyItemData()).name = loc12.name;
                                    loc8.type = loc12.id;
                                    loc8.typeName = MWPropertyItemData.TypeName_Daemon;
                                    loc8.property = int(loc6[1]);
                                    loc7.push(loc8);
                                }
                            }
                            ++loc4;
                        }
                    }
                }
            }
            return loc7;
        }

        public static nextLvProperty(arg1: ItemData, arg2: int, arg3: int, arg4: Boolean=false, arg5: Boolean=false, arg6: Boolean=false): Array<any>
        {
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc5=0;
            var loc6=NaN;
            var loc7=NaN;
            var loc8=null;
            var loc9=0;
            var loc10=null;
            var loc11=null;
            var loc12=null;
            var loc13=0;
            var loc1=[];
            var loc14;
            if (loc14 = MagicWeaponUtil.analyseProperty(arg1, arg6)) 
            {
                loc11 = MagicWeaponConfig.instance.getItemByTypeColorLevel(5, arg2, arg3);
                loc12 = MagicWeaponConfig.instance.getItemByTypeColorLevel(5, arg2, (arg3 - 1));
                if (loc11) 
                {
                    loc6 = loc11.add / 100;
                }
                if (loc12) 
                {
                    loc7 = loc12.add / 100;
                }
                loc4 = loc14.length;
                loc5 = 0;
                while (loc5 < loc4) 
                {
                    if (loc3 = loc14[loc5] as MWPropertyItemData) 
                    {
                        loc9 = Math.floor(loc3.property * loc6);
                        loc13 = Math.floor(loc3.property * loc7);
                        (loc2 = new MWPropertyItemData()).name = loc3.name;
                        loc2.property = loc3.property + loc9;
                        loc2.addNum = loc9 - loc13;
                        loc2.isAdvance = loc9 > 0;
                        loc2.isUpgradeSucceed = arg5;
                        if (loc14 && !(loc6 == 0) && arg4) 
                        {
                            loc2.needShowArrow = true;
                        }
                        else 
                        {
                            loc2.needShowArrow = false;
                        }
                        loc1.push(loc2);
                    }
                    ++loc5;
                }
            }
            return loc1;
        }

        public static magicWeaponCanXiLian(arg1: ItemData): Boolean
        {
            if (arg1) 
            {
                if (!(arg1.posType == EPlayerItemPosType._EPlayerItemPosTypeRole) && arg1.magicWeaponLv > 0) 
                {
                    return true;
                }
                if (arg1.posType == EPlayerItemPosType._EPlayerItemPosTypeRole) 
                {
                    return true;
                }
            }
            return false;
        }

        public static sortData(arg1: MagicWeaponItemData, arg2: MagicWeaponItemData): int
        {
            if (arg1.itemData.magicWeaponLv >= arg2.itemData.magicWeaponLv) 
            {
                if (arg1.itemData.color >= arg2.itemData.color) 
                {
                    if (arg1.itemData.magicWeaponScore > arg2.itemData.magicWeaponScore) 
                    {
                        return -1;
                    }
                    if (arg1.itemData.magicWeaponScore == arg2.itemData.magicWeaponScore) 
                    {
                        if (arg1.itemData.color > arg2.itemData.color) 
                        {
                            return -1;
                        }
                        if (arg1.itemData.color == arg2.itemData.color && arg1.itemData.magicWeaponLv > arg2.itemData.magicWeaponLv) 
                        {
                            return -1;
                        }
                        return 0;
                    }
                    if (arg1.itemData.color > arg2.itemData.color) 
                    {
                        return -1;
                    }
                    if (arg1.itemData.color == arg2.itemData.color && arg1.itemData.magicWeaponLv > arg2.itemData.magicWeaponLv) 
                    {
                        return -1;
                    }
                    return 1;
                }
                if (arg1.itemData.magicWeaponLv > arg2.itemData.magicWeaponLv) 
                {
                    return -1;
                }
                return 1;
            }
            return 1;
        }

        public static getMainProperty(arg1: ItemData): int
        {
            var loc5=null;
            var loc6=0;
            if (arg1 == null || !arg1.isHasIdentify()) 
            {
                return 0;
            }
            var loc1=MagicWeaponConfig.instance.getItemByTypeColorLevel(5, arg1.color, arg1.magicWeaponLv);
            var loc2=0;
            if (loc1 != null) 
            {
                loc2 = loc1.add;
            }
            var loc3=MagicWeaponUtil.analyseProperty(arg1);
            var loc4=0;
            while (!(loc3 == null) && loc4 < loc3.length) 
            {
                if ((loc5 = loc3[loc4] as MWPropertyItemData) != null) 
                {
                    loc6 = int(loc5.property * loc2 / 100);
                    if (loc5.typeName == MWPropertyItemData.TypeName_Main) 
                    {
                        return loc5.property + loc6;
                    }
                }
                ++loc4;
            }
            return 0;
        }

        public static getTotalMagicWeaponWuXingFight(arg1: Array<any>): int
        {
            var loc3=null;
            var loc4=null;
            var loc6=0;
            var loc1=arg1.length;
            var loc2=[];
            var loc5=[];
            var loc7=0;
            while (loc7 < loc1) 
            {
                if ((loc3 = arg1[loc7] as ItemData) && !(loc3 == ItemData.nullItemData) && loc3.category == ECategory._ECategoryAmulet) 
                {
                    loc2 = MagicWeaponUtil.nextLvProperty(loc3, loc3.color, loc3.magicWeaponLv);
                    if (loc2.length > 0) 
                    {
                        loc4 = loc2[0] as MWPropertyItemData;
                        loc5.push(loc4);
                    }
                }
                ++loc7;
            }
            var loc8=loc5.length;
            var loc9=0;
            while (loc9 < loc8) 
            {
                loc6 = loc6 + (loc5[loc9] as MWPropertyItemData).property;
                ++loc9;
            }
            return loc6;
        }

        public static isNoPropertyWeapon(arg1: ItemData): Boolean
        {
            return arg1.magicWeaponScore <= 0 && arg1.magicWeaponLv <= 0;
        }

        public static isFaultItem(arg1: ItemData): Boolean
        {
            return false;
        }

        public static isShowMask(arg1: ItemData): Boolean
        {
            if (MagicWeaponUtil.isFaultItem(arg1)) 
            {
                if (arg1.itemExInfo) 
                {
                    if (arg1.posType == EPlayerItemPosType._EPlayerItemPosTypeWarehouse || arg1.posType == EPlayerItemPosType._EPlayerItemPosTypeTreasure) 
                    {
                        return true;
                    }
                    if (arg1.posType == EPlayerItemPosType._EPlayerItemPosTypeBag) 
                    {
                        if (Cache.instance.pack.backPackCache.getItemDataByUid(arg1.uid) != null) 
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public static get isInUpgradeAmuletScore(): Boolean
        {
            return Cache.instance.active.isActiveInEffect(EActiveType._EActiveTypeUpgradeAmuletScore);
        }

        public static isMagicWeaponOpenInPackItem(arg1: ItemData): Boolean
        {
            if (ItemsUtil.isAmuletIdentifyStone(arg1) || ItemsUtil.isAmuletValueStone(arg1) || ItemsUtil.isAmuletActiveItem(arg1)) 
            {
                return true;
            }
            if (ItemsUtil.isUpgradeAmuletScoreItem(arg1)) 
            {
                if (isInUpgradeAmuletScore) 
                {
                    return true;
                }
                MsgManager.showRollTipsMsg(Language.getString(28506));
            }
            return false;
        }
    }
